using System;
using System.Data;
using System.Collections;
using System.Collections.Generic;

using Framework;

namespace SchemaDeploy
{
    [Serializable()]
    public partial class CBackupItem : CBaseDynamic, IComparable<CBackupItem>
    {
        #region Constructors
        //Public (Copy Constructor)
        public CBackupItem(CBackupItem original, CDataSrc target)
        {
            //Database Connection
            m_dataSrc = target;

            //Copy fields
            _itemBackupId = original.ItemBackupId;
            _itemTableName = original.ItemTableName;
            _itemDatasetXmlGz = original.ItemDatasetXmlGz;
            _itemDatasetMD5 = original.ItemDatasetMD5;
            _size = original.Size;
            _itemSchemaMD5 = original.ItemSchemaMD5;
            _itemSchemaXmlGz = original.ItemSchemaXmlGz;
            _backupCreated = original.BackupCreated;
            _backupInstanceId = original.BackupInstanceId;
            _instanceClientName = original.InstanceClientName;
            _instanceSuffix = original.InstanceSuffix;
            _dataBytes = original.DataBytes;
            _schemaBytes = original.SchemaBytes;
            _itemRowCount = original.ItemRowCount;
        }

        //Protected (Datareader/Dataset)
        protected CBackupItem(CDataSrc dataSrc, IDataReader dr) : base(dataSrc, dr) {}
        protected CBackupItem(CDataSrc dataSrc, DataRow     dr) : base(dataSrc, dr) {}
        #endregion

        #region Default Values
        protected override void InitValues_Auto()
        {
            //Null-Equivalent values (except String.Empty, as nulls tend to be inconvenient for string types)
            _itemId = int.MinValue;
            _itemBackupId = int.MinValue;
            _itemTableName = string.Empty;
            _itemRowCount = int.MinValue;
            _itemDatasetXmlGz = new byte[]{};
            _itemDatasetMD5 = Guid.Empty;
            _itemSchemaXmlGz = new byte[]{};
            _itemSchemaMD5 = Guid.Empty;
        }
        #endregion

        #region Members
        protected int _itemId;
        protected int _itemBackupId;
        protected string _itemTableName;
        protected byte[] _itemDatasetXmlGz;
        protected Guid _itemDatasetMD5;
        protected long _size;
        protected Guid _itemSchemaMD5;
        protected byte[] _itemSchemaXmlGz;
        protected DateTime _backupCreated;
        protected int _backupInstanceId;
        protected string _instanceClientName;
        protected string _instanceSuffix;
        protected long _dataBytes;
        protected long _schemaBytes;
        protected int _itemRowCount;
        #endregion

        #region Properties - Column Values
        //Primary Key Column (ReadOnly)
        public int ItemId    {   get { return _itemId;   } }

        //Table Columns (Read/Write)
        public int ItemBackupId    {   get { return _itemBackupId; } set { _itemBackupId = value; }    }
        public string ItemTableName    {   get { return _itemTableName; } set { _itemTableName = value; }    }
        public int ItemRowCount    {   get { return _itemRowCount; } set { _itemRowCount = value; }    }
        public byte[] ItemDatasetXmlGz    {   get { return _itemDatasetXmlGz; } set { _itemDatasetXmlGz = value; }    }
        public Guid ItemDatasetMD5    {   get { return _itemDatasetMD5; } set { _itemDatasetMD5 = value; }    }
        public byte[] ItemSchemaXmlGz    {   get { return _itemSchemaXmlGz; } set { _itemSchemaXmlGz = value; }    }
        public Guid ItemSchemaMD5    {   get { return _itemSchemaMD5; } set { _itemSchemaMD5 = value; }    }

        //View Columns (ReadOnly)
        public long Size    {   get { return _size;   } }
        public DateTime BackupCreated    {   get { return _backupCreated;   } }
        public int BackupInstanceId    {   get { return _backupInstanceId;   } }
        public string InstanceClientName    {   get { return _instanceClientName;   } }
        public string InstanceSuffix    {   get { return _instanceSuffix;   } }
        public long DataBytes    {   get { return _dataBytes;   } }
        public long SchemaBytes    {   get { return _schemaBytes;   } }

        #endregion

        #region MustOverride Methods
        //Schema Information
        public const string TABLE_NAME      = "Deploy_BackupItem";
        public const string VIEW_NAME       = "vwDeploy_BackupItem_NullBinGz";         //Used to override this.ViewName { get }
        public const string ORDER_BY_COLS   = "ItemId DESC";   //See the CompareTo method below (Sql-based sorting should match In-Memory sorting)
        public const string SORTING_COLUMN  = "";
        public override string TableName {    get { return TABLE_NAME; } }
        protected override string ViewName {    get {   return VIEW_NAME;    }    }
        protected override string OrderByColumns {    get { return ORDER_BY_COLS; } }

        //CompareTo Interface (Default Sort Order)
        public int CompareTo(CBackupItem other) {   return this.ItemId.CompareTo(other.ItemId) *-1;  }

        //Primary Key Information
        public const string PRIMARY_KEY_NAME = "ItemId";
        protected override bool InsertPrimaryKey {  get { return false;    }   }
        protected override string PrimaryKeyName {  get { return PRIMARY_KEY_NAME;    }    }
        protected override object PrimaryKeyValue 
        {
              get { return _itemId; }
              set { _itemId = (int)value; }
        }

        //Factory Methods - Object
        protected override CBase MakeFrom(DataRow row)      {   return new CBackupItem(this.DataSrc, row);  }
        protected override CBase MakeFrom(IDataReader dr)   {   return new CBackupItem(this.DataSrc, dr);   }

        //Factory Methods - List
        protected override IList MakeList()                 {   return new CBackupItemList();               }
        protected override IList MakeList(int capacity)     {   return new CBackupItemList(capacity);       }

        //Convert from ADO to .Net
        protected override void ReadColumns(IDataReader dr)
        {
            _itemId = CAdoData.GetInt(dr, "ItemId");
            _itemBackupId = CAdoData.GetInt(dr, "ItemBackupId");
            _itemTableName = CAdoData.GetStr(dr, "ItemTableName");
            _itemDatasetXmlGz = CAdoData.GetBytes(dr, "ItemDatasetXmlGz");
            _itemDatasetMD5 = CAdoData.GetGuid(dr, "ItemDatasetMD5");
            _size = CAdoData.GetLong(dr, "Size");
            _itemSchemaMD5 = CAdoData.GetGuid(dr, "ItemSchemaMD5");
            _itemSchemaXmlGz = CAdoData.GetBytes(dr, "ItemSchemaXmlGz");
            _backupCreated = CAdoData.GetDate(dr, "BackupCreated");
            _backupInstanceId = CAdoData.GetInt(dr, "BackupInstanceId");
            _instanceClientName = CAdoData.GetStr(dr, "InstanceClientName");
            _instanceSuffix = CAdoData.GetStr(dr, "InstanceSuffix");
            _dataBytes = CAdoData.GetLong(dr, "DataBytes");
            _schemaBytes = CAdoData.GetLong(dr, "SchemaBytes");
            _itemRowCount = CAdoData.GetInt(dr, "ItemRowCount");
        }
        protected override void ReadColumns(DataRow dr)
        {
            _itemId = CAdoData.GetInt(dr, "ItemId");
            _itemBackupId = CAdoData.GetInt(dr, "ItemBackupId");
            _itemTableName = CAdoData.GetStr(dr, "ItemTableName");
            _itemDatasetXmlGz = CAdoData.GetBytes(dr, "ItemDatasetXmlGz");
            _itemDatasetMD5 = CAdoData.GetGuid(dr, "ItemDatasetMD5");
            _size = CAdoData.GetLong(dr, "Size");
            _itemSchemaMD5 = CAdoData.GetGuid(dr, "ItemSchemaMD5");
            _itemSchemaXmlGz = CAdoData.GetBytes(dr, "ItemSchemaXmlGz");
            _backupCreated = CAdoData.GetDate(dr, "BackupCreated");
            _backupInstanceId = CAdoData.GetInt(dr, "BackupInstanceId");
            _instanceClientName = CAdoData.GetStr(dr, "InstanceClientName");
            _instanceSuffix = CAdoData.GetStr(dr, "InstanceSuffix");
            _dataBytes = CAdoData.GetLong(dr, "DataBytes");
            _schemaBytes = CAdoData.GetLong(dr, "SchemaBytes");
            _itemRowCount = CAdoData.GetInt(dr, "ItemRowCount");
        }

        #endregion

        #region Queries - SelectAll/SelectWhere (inherited methods, cast only)
        //Select Queries - Non-Paged
        public new CBackupItemList SelectAll()                                                          {    return (CBackupItemList)base.SelectAll();                              }
        public new CBackupItemList SelectAll(string orderBy)                                            {    return (CBackupItemList)base.SelectAll(orderBy);                       }
        public new CBackupItemList SelectWhere(string colName, ESign sign, object colValue)             {    return (CBackupItemList)base.SelectWhere(colName, sign, colValue);     }
        public new CBackupItemList SelectWhere(CCriteria where)                                         {    return (CBackupItemList)base.SelectWhere(where);                       }
        public new CBackupItemList SelectWhere(CCriteriaList where)                                     {    return (CBackupItemList)base.SelectWhere(where);                       }
        public new CBackupItemList SelectWhere(CCriteriaList where, string tableOrJoin)                 {    return (CBackupItemList)base.SelectWhere(where, tableOrJoin);          }
        public new CBackupItemList SelectWhere(CCriteriaList where, string tableOrJoin, string orderBy) {    return (CBackupItemList)base.SelectWhere(where, tableOrJoin, orderBy); }
        [Obsolete("Arbitrary where-clause is not necessary portable or safe from sql injection attacks. Consider using the parameterised query interfaces")]
        public new CBackupItemList SelectWhere(string unsafeWhereClause)                                {    return (CBackupItemList)base.SelectWhere(unsafeWhereClause);           }
        public     CBackupItemList SelectById(int itemId)                              {    return (CBackupItemList)base.SelectById(itemId);       }
        public     CBackupItemList SelectByIds(List<int> ids)                        {    return (CBackupItemList)base.SelectByIds(ids);                         }

        //Select Queries - Paged
        public new CBackupItemList SelectAll(  CPagingInfo pi)                                              {    return (CBackupItemList)base.SelectAll(  pi);                              }
        public new CBackupItemList SelectWhere(CPagingInfo pi, string name, ESign sign, object value)       {    return (CBackupItemList)base.SelectWhere(pi, name, sign, value);           }
        public new CBackupItemList SelectWhere(CPagingInfo pi, CCriteria criteria)                          {    return (CBackupItemList)base.SelectWhere(pi, criteria);                    }
        public new CBackupItemList SelectWhere(CPagingInfo pi, CCriteriaList criteria)                      {    return (CBackupItemList)base.SelectWhere(pi, criteria);                    }
        public new CBackupItemList SelectWhere(CPagingInfo pi, CCriteriaList criteria, string viewOrJoin)   {    return (CBackupItemList)base.SelectWhere(pi, criteria, viewOrJoin);        }
        public     CBackupItemList SelectByIds(CPagingInfo pi, List<int> ids)            {    return (CBackupItemList)base.SelectByIds(pi, ids);                         }

        //Select Queries - Transactional
        public new CBackupItemList SelectAll(                                                                                           IDbTransaction tx)  {    return (CBackupItemList)base.SelectAll(tx);                                                     }
        public new CBackupItemList SelectAll(string orderBy,                                                                            IDbTransaction tx)  {    return (CBackupItemList)base.SelectAll(orderBy,                                         tx);    }
        public new CBackupItemList SelectWhere(string columnName,               object columnValue,                                     IDbTransaction tx)  {    return (CBackupItemList)base.SelectWhere(columnName,              columnValue,          tx);    }
        public new CBackupItemList SelectWhere(string columnName,   ESign sign, object columnValue,                                     IDbTransaction tx)  {    return (CBackupItemList)base.SelectWhere(columnName,    sign,     columnValue,          tx);    }
        public new CBackupItemList SelectWhere(                             CCriteria     criteria,                                     IDbTransaction tx)  {    return (CBackupItemList)base.SelectWhere(               criteria,                       tx);    }
        public new CBackupItemList SelectWhere(                             CCriteriaList criteria,                                     IDbTransaction tx)  {    return (CBackupItemList)base.SelectWhere(               criteria,                       tx);    }
        public new CBackupItemList SelectWhere(                             CCriteriaList criteria, string tableOrJoin,                 IDbTransaction tx)  {    return (CBackupItemList)base.SelectWhere(               criteria, tableOrJoin,          tx);    }
        public new CBackupItemList SelectWhere(                             CCriteriaList criteria, string tableOrJoin, string orderBy, IDbTransaction tx)  {    return (CBackupItemList)base.SelectWhere(               criteria, tableOrJoin,          tx);    }
        public     CBackupItemList SelectById(int itemId,                                                              IDbTransaction tx)  {    return (CBackupItemList)base.SelectById(itemId,                         tx);    }
        public     CBackupItemList SelectByIds(List<int> ids,                                                        IDbTransaction tx)  {    return (CBackupItemList)base.SelectByIds(ids,                                           tx);    }

        //Select Queries - Stored Procs
        public new CBackupItemList MakeList(string storedProcName,                             IDbTransaction txOrNull)    {   return (CBackupItemList)base.MakeList(storedProcName,             txOrNull);  }
        public new CBackupItemList MakeList(string storedProcName, object[] parameters,        IDbTransaction txOrNull)    {   return (CBackupItemList)base.MakeList(storedProcName, parameters, txOrNull);  }
        public new CBackupItemList MakeList(string storedProcName, CNameValueList parameters,  IDbTransaction txOrNull)    {   return (CBackupItemList)base.MakeList(storedProcName, parameters, txOrNull);  }
        public new CBackupItemList MakeList(string storedProcName, List<object> parameters,    IDbTransaction txOrNull)    {   return (CBackupItemList)base.MakeList(storedProcName, parameters, txOrNull);  }
        public new CBackupItemList MakeList(string storedProcName, int param1,                 IDbTransaction txOrNull)    {   return (CBackupItemList)base.MakeList(storedProcName, param1,     txOrNull);  }
        public new CBackupItemList MakeList(string storedProcName, string param1,              IDbTransaction txOrNull)    {   return (CBackupItemList)base.MakeList(storedProcName, param1,     txOrNull);  }

        //Query Results
        protected new CBackupItemList MakeList(DataSet              ds) { return (CBackupItemList)base.MakeList(ds);        }
        protected new CBackupItemList MakeList(DataTable            dt) { return (CBackupItemList)base.MakeList(dt);        }
        protected new CBackupItemList MakeList(DataRowCollection  rows) { return (CBackupItemList)base.MakeList(rows);      }
        protected new CBackupItemList MakeList(IDataReader          dr) { return (CBackupItemList)base.MakeList(dr);        }
        protected new CBackupItemList MakeList(object           drOrDs) { return (CBackupItemList)base.MakeList(drOrDs);    }        
        protected new CBackupItemList MakeList(byte[]             gzip) { return (CBackupItemList)base.MakeList(gzip);      }
        #endregion

        #region Queries - SelectBy[FK] (user-nominated fk/bool columns)
        //Non-Paged
        public CBackupItemList SelectByBackupId(int itemBackupId)    {    return SelectWhere(new CCriteriaList("ItemBackupId", itemBackupId));    }
        public CBackupItemList SelectByTableName(string itemTableName)    {    return SelectWhere(new CCriteriaList("ItemTableName", itemTableName));    }
        public CBackupItemList SelectByDatasetMD5(Guid itemDatasetMD5)    {    return SelectWhere(new CCriteriaList("ItemDatasetMD5", itemDatasetMD5));    }
        public CBackupItemList SelectBySchemaMD5(Guid itemSchemaMD5)    {    return SelectWhere(new CCriteriaList("ItemSchemaMD5", itemSchemaMD5));    }

        //Paged
        public CBackupItemList SelectByBackupId(CPagingInfo pi, int itemBackupId)    {    return SelectWhere(pi, new CCriteriaList("ItemBackupId", itemBackupId));    }
        public CBackupItemList SelectByTableName(CPagingInfo pi, string itemTableName)    {    return SelectWhere(pi, new CCriteriaList("ItemTableName", itemTableName));    }
        public CBackupItemList SelectByDatasetMD5(CPagingInfo pi, Guid itemDatasetMD5)    {    return SelectWhere(pi, new CCriteriaList("ItemDatasetMD5", itemDatasetMD5));    }
        public CBackupItemList SelectBySchemaMD5(CPagingInfo pi, Guid itemSchemaMD5)    {    return SelectWhere(pi, new CCriteriaList("ItemSchemaMD5", itemSchemaMD5));    }

        //Count
        public int SelectCountByBackupId(int itemBackupId)   {   return SelectCount(new CCriteriaList("ItemBackupId", itemBackupId));     }
        public int SelectCountByTableName(string itemTableName)   {   return SelectCount(new CCriteriaList("ItemTableName", itemTableName));     }
        public int SelectCountByDatasetMD5(Guid itemDatasetMD5)   {   return SelectCount(new CCriteriaList("ItemDatasetMD5", itemDatasetMD5));     }
        public int SelectCountBySchemaMD5(Guid itemSchemaMD5)   {   return SelectCount(new CCriteriaList("ItemSchemaMD5", itemSchemaMD5));     }

        //Transactional
        public CBackupItemList SelectByBackupId(int itemBackupId, IDbTransaction tx)    {    return SelectWhere(new CCriteriaList("ItemBackupId", itemBackupId), tx);    }
        public CBackupItemList SelectByTableName(string itemTableName, IDbTransaction tx)    {    return SelectWhere(new CCriteriaList("ItemTableName", itemTableName), tx);    }
        public CBackupItemList SelectByDatasetMD5(Guid itemDatasetMD5, IDbTransaction tx)    {    return SelectWhere(new CCriteriaList("ItemDatasetMD5", itemDatasetMD5), tx);    }
        public CBackupItemList SelectBySchemaMD5(Guid itemSchemaMD5, IDbTransaction tx)    {    return SelectWhere(new CCriteriaList("ItemSchemaMD5", itemSchemaMD5), tx);    }
        #endregion

        #region ToXml
        protected override void ToXml_Autogenerated(System.Xml.XmlWriter w)
        {
            Store(w, "ItemId", this.ItemId);
            Store(w, "ItemBackupId", this.ItemBackupId);
            Store(w, "ItemTableName", this.ItemTableName);
            Store(w, "ItemRowCount", this.ItemRowCount);
            Store(w, "ItemDatasetXmlGz", this.ItemDatasetXmlGz);
            Store(w, "ItemDatasetMD5", this.ItemDatasetMD5);
            Store(w, "ItemSchemaXmlGz", this.ItemSchemaXmlGz);
            Store(w, "ItemSchemaMD5", this.ItemSchemaMD5);
        }
        #endregion

    }
}
